/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry;

import com.google.gwt.core.client.GWT;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.BlogEntryUpdateDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.events.out.BlogEntrySuccessfullAddEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.events.out.BlogEntrySuccessfullRemovedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.events.out.BlogEntrySuccessfullUpdatedEvent;
import com.googlecode.hmvc4gwt.framework.client.rpc.ASessionAsyncCallback;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.IEventListener;

/**
 * 
 * @author jens.meiss
 */
public class BlogEntryAsyncCallback<T, E extends IEvent> extends
		ASessionAsyncCallback<T, E> {
	
	private static IBlogEntryRemoteServiceAsync remoteService;
	
	/**
	 * @return the remoteService
	 */
	private static IBlogEntryRemoteServiceAsync getRemoteService() {
		
		if (remoteService == null) {
			setRemoteService((IBlogEntryRemoteServiceAsync) GWT.create(IBlogEntryRemoteService.class));
		}
		
		return remoteService;
	}

	/**
	 * @param remoteService the remoteService to set
	 */
	private static void setRemoteService(IBlogEntryRemoteServiceAsync remoteService) {
		BlogEntryAsyncCallback.remoteService = remoteService;
	}

	public static void add(BlogEntryNewDTO blogEntryNewDTO, IEventListener eventListener) {
		
		BlogEntryAsyncCallback<Void, BlogEntrySuccessfullAddEvent> callback = new BlogEntryAsyncCallback<Void, BlogEntrySuccessfullAddEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogEntrySuccessfullAddEvent());
		
		blogEntryNewDTO.setSessionId(getSessionId());
		
		getRemoteService().addBlogEntry(blogEntryNewDTO, callback);
	}
	
	public static void update(BlogEntryUpdateDTO blogEntryUpdateDTO, IEventListener eventListener) {
		
		BlogEntryAsyncCallback<Void, BlogEntrySuccessfullUpdatedEvent> callback = new BlogEntryAsyncCallback<Void, BlogEntrySuccessfullUpdatedEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogEntrySuccessfullUpdatedEvent());
		
		blogEntryUpdateDTO.setSessionId(getSessionId());
		
		getRemoteService().updateBlogEntry(blogEntryUpdateDTO, callback);
		
	}
	
	public static void remove(BlogEntryRemoveDTO blogEntryRemoveDTO, IEventListener eventListener) {
		
		BlogEntryAsyncCallback<Void, BlogEntrySuccessfullRemovedEvent> callback = new BlogEntryAsyncCallback<Void, BlogEntrySuccessfullRemovedEvent>();
		callback.setEventListener(eventListener);
		callback.setEventOnSuccess(new BlogEntrySuccessfullRemovedEvent());
		
		blogEntryRemoveDTO.setSessionId(getSessionId());
		
		getRemoteService().removeBlogEntry(blogEntryRemoveDTO, callback);
	}

}
